#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "Parser.h"
#include "Search.h"

int strcmp_device2 (char *word, char *value, int index, int size)
{
	for(int i = index; i < size; i++){
		if(word[i] == ',' && value[i - index] == '\0')
			return 0;
		if(word[i] < value[i - index])
			return -1;
		else
		if(word[i] > value[i - index])
			return 1;
	}
	return 1;
}


int search_kernel2(char *str, char *value, Map *map, int max, int size, int *index) {

 int position;
 int begin = 0; 
 int end = max - 1;
 int cond = 0;

 while(begin <= end) {
  position = (begin + end) / 2;
  cond = strcmp_device2(str, value, map[position].addBeg, size);
  *index = position;
  if(cond == 0){
	if(map[position].done == 1)
		return 0;
	else{
		//atomicCAS(&(map[position].done), 0, 1);
		map[position].done = 1;
		return 1;
		}
	}
  else if(cond < 0)
   begin = position + 1;
  else
   end = position - 1;
 }
 return 0;
}


void parser_kernel2 (Map *maps, char *block, int size)
{
	int j = 0;
	int index = 0;
	maps[0].addBeg = 0;
	
	for(int i = 0; i < size; i++){
		if(block[i] == ','){
			maps[index].done = 0;
			index++;
			maps[index].addBeg = i + 1;
				
		}
	}

}

void setup_kernel2 ()
{
	srand(time(NULL));
}

void generate_kernel2(char *block, Map *map, int totalWords, int size, int *result, int lengh)
{
	int index;
	
	char word[50];
	
		for(int i = 0; i < lengh; i++)
			word[i] = rand() % 26 + 97;
		
		word[lengh] = '\0';

		int cond = search_kernel2(block, word, map, totalWords, size, &index);
		if(cond){
			//atomicInc((unsigned int *)result, totalWords + 1);
			(*result)++;
			//printf("Achou - %d - %s\n", id, word);
		}
}



int sequencial ()
{
	//Host statements
	FILE *file;
	int size, bytes;
	unsigned int totalWords, newWords;
	char *block, *aux;
	char **words;
	int *wordSize;
	
	
	
	totalWords = 0;
	newWords = 0;
	
	file = fopen("palavras.txt", "r");
	
	fseek (file , 0 , SEEK_END);
	size = ftell (file);
	rewind (file);
	
	printf("Reading from the main file...\n");
	
	block = (char *)malloc((size) * sizeof(char));
	bytes = fread(block, sizeof(char), size, file);
	
	words = (char **)malloc((size/(MAXWORD - 1) + 2800000) * sizeof(char *));
	for(unsigned int i = 0; i < (size/(MAXWORD - 1) + 2800000); i++)
		words[i] = (char *) malloc((MAXWORD + 1) * sizeof(char));
		
	parse(words, &totalWords, block, bytes);
	
	sort(words, totalWords);
	
	bytes = 0;
	
	block = (char *)malloc((size/38) * sizeof(char));
	
	block[0] = '\0';
	
	wordSize = (int *) calloc(MAXWORD + 1, sizeof(int));
	
	printf("Filtering the main file...\n");
	int lengh;
	
	for(int i = 1; i < totalWords; i++)
	{
		if(strcmp(words[i], words[i-1]) != 0)
		{
			strcat(block, words[i-1]);
			strcat(block, ",");
			lengh = strlen(words[i-1]);
			wordSize[lengh]++;
			bytes += lengh + 1;
			newWords++;
		}
	}
	
	block[bytes] = '\0';
	
	fclose(file);
	free(words);
	
	////////////////////////////////////////////////////////////////////////
	
	Map *devMaps;
	int result = 0;

	devMaps = (Map *)malloc(newWords * sizeof(Map));
	
	printf("Words parsing...\n");
	
	parser_kernel2(devMaps, block, bytes);
	
	setup_kernel2();
	
	printf("Words generation\n");
	
	wordsAdjustment(&newWords);
	
	float percentage = 0.1;
	
	time_t timeTotal[11];
	timeTotal[0] = time(NULL);
	int j = 1;
	
	while(result < newWords){
		
		generate_kernel2(block, devMaps, newWords, bytes, &result, 1);
		generate_kernel2(block, devMaps, newWords, bytes, &result, 2);
		generate_kernel2(block, devMaps, newWords, bytes, &result, 3);
		generate_kernel2(block, devMaps, newWords, bytes, &result, 4);
		generate_kernel2(block, devMaps, newWords, bytes, &result, 5);
		
		if(result >= percentage * newWords){
			timeTotal[j] = time(NULL) - timeTotal[0];
			j++;
			printf("Found %.0f per cent of the words\n", percentage * 100);
			percentage += 0.1;
		}
		
	}
	
	timeTotal[10] = time(NULL) - timeTotal[0];
	printf("Found 100 per cent of the words\n");
	
	for(int i = 1; i < 11; i++)
		printf("Time of %d per cent - %ld\n", i * 10, timeTotal[i]); 
	
	free(devMaps);
	free(block);
	
	

system("PAUSE");
return 0;
}